//
//  SearchResultVC.m
//  BrushFace
//
//  Created by timer_q on 16/1/22.
//  Copyright © 2016年 Zzz. All rights reserved.
//

#import "PassFriendVC.h"
#import "SearchAddVC.h"
#import "ContactViewModel.h"
#import "DetailInfoCell.h"
#import "DetailDefaultCell.h"
#import "VerfifyFriendCellTableViewCell.h"


@interface PassFriendVC ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView             *_tableView;
    NSArray                 *_cityArr;
    NSDictionary            *_currentDict;
}
@property (nonatomic,strong)ContactViewModel *reqModel;
@end

@implementation PassFriendVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.navBar setTitle:@"详细资料"];
    [self.navBar setLeftBtn:NAV_BAR_LEFT_BTN_TYPE_BLUE];
    [self UIConfig];
    [self dataInit];
}
- (void)UIConfig{
    self.view.backgroundColor = UIColorFromRGB(ColorValueForBackView_Gray);
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 65, SCREEN_WIDTH, 320) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = UIColorFromRGB(ColorValueForBackView_Gray);
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_reqModel fetchFriendDetailWithRequestWithUserId:self.userid andCB:^(id output) {
        _currentDict = output;
        [_tableView reloadData];
    }];
    
}
- (void)dataInit{
    _reqModel = [ContactViewModel SceneModel];
    
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"address" ofType:@"json"];
    NSData *data = [[NSData alloc]initWithContentsOfFile:plistPath];
    id jsondata = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    _cityArr = (NSArray *)jsondata;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        DetailInfoCell *cell = [DetailInfoCell loadBundleNib];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.headerImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",HEADERIMAGE_FETCH,_currentDict[@"userid"]]]];
        cell.nickNameLabel.text = _currentDict[@"nickname"];
        NSInteger fullnamed = [[NSString stringWithFormat:@"%@",_currentDict[@"fullnamed"]] integerValue];
        if (fullnamed == 1) {
            cell.fullNameLabel.hidden = NO;
        }else{
            cell.fullNameLabel.hidden = YES;
        }
        NSString *homeCity;
        NSString *liveCity;
        NSString *homep;
        NSString *liveP;
        for (NSDictionary *subDict in _cityArr) {
            if ([_currentDict[@"homecity"] isNotEmpty]) {
                if ([[subDict objectForKey:@"value"] isEqualToString:_currentDict[@"homecity"]]) {
                    homep = subDict[@"name"];
                    NSArray * homeCityArr = [subDict objectForKey:@"items"];
                    for (NSDictionary *homeCityDict in homeCityArr) {
                        if ([[homeCityDict objectForKey:@"value"] isEqualToString:_currentDict[@"homeprovince"]]) {
                            homeCity = homeCityDict[@"name"];
                        }
                    }
                }
            }else{
                homep = @"未知";
                homeCity = @"未知";
            }
            
            if ([_currentDict[@"liveprovince"] isNotEmpty]) {
                
                if ([[subDict objectForKey:@"value"] isEqualToString:_currentDict[@"liveprovince"]]) {
                    liveP = subDict[@"name"];
                    NSArray * homeCityArr = [subDict objectForKey:@"items"];
                    for (NSDictionary *homeCityDict in homeCityArr) {
                        if ([[homeCityDict objectForKey:@"value"] isEqualToString:_currentDict[@"livecity"]]) {
                            liveCity = homeCityDict[@"name"];
                        }
                    }
                }
            }else{
                liveCity = @"未知";
                liveP = @"未知";
            }
        }
        
        cell.habitatLabel.text = [NSString stringWithFormat:@"居住地：%@-%@",liveP,liveCity];
        cell.hometownLabel.text = [NSString stringWithFormat:@"家乡：%@-%@",homep,homeCity];
        
        return cell;
    }
    if (indexPath.section ==1) {
        VerfifyFriendCellTableViewCell *cell = [VerfifyFriendCellTableViewCell loadBundleNib];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.cellMsg.text = [NSString stringWithFormat:@"%@",_currentDict[@"message"]];
        return cell;
    }
    if (indexPath.section == 2) {
        DetailDefaultCell *cell = [DetailDefaultCell loadBundleNib];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.nextArrow.hidden = YES;
        cell.title.text = @"来源";
        cell.detail.text = @"未知";
        if ([[NSString stringWithFormat:@"%@", _currentDict[@"source"]] integerValue]==1) {
            cell.detail.text = @"搜索手机号";
        }
        if ([[NSString stringWithFormat:@"%@", _currentDict[@"source"]] integerValue]==2) {
            cell.detail.text = @"通讯录匹配";
        }
        if ([[NSString stringWithFormat:@"%@", _currentDict[@"source"]] integerValue]==3) {
            cell.detail.text = @"微信";
        }
        return cell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 150;
    }
    if (indexPath.section == 1) {
        return 80;
    }
    return 44;
}
- (IBAction)addFriendBtnClikc:(id)sender {
    NSString *requestStr = [NSString stringWithFormat:@"%@/addressbook/agreeToMakeFriend.json",SERVICE_ADRESS];
    NSDictionary *params = @{@"frienduserid":_currentDict[@"userid"]};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
    [manager GET:requestStr parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        if ([[NSString stringWithFormat:@"%@",jsonObject[@"result"]] integerValue]==1) {
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:jsonObject[@"errormsg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }

    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
    }];
    
}

@end
